package com.cupcorn.lib.display;

import org.andengine.entity.scene.Scene;
import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.exception.TMXLoadException;

import com.cupcorn.lib.Utils;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.element.GameElementTmx;

public class DisplayTmx
{
	GameEngine			engine;
	GameElementTmx[]	gameElementTmx;
	TMXTiledMap[]		tiledMap;
	
	public DisplayTmx(final GameEngine engine, GameElementTmx[] gameElementTmx)
	{
		this.engine = engine;
		
		tiledMap = new TMXTiledMap[gameElementTmx.length];
		
		for (int i = 0; i < gameElementTmx.length; i++)
		{
			try
			{
				Utils.TRACE(" i = " + i);
				tiledMap[i] =
					new TMXLoader(engine.getAssets(), engine.getTextureManager(), engine.getVertexBufferObjectManager()).loadFromAsset(gameElementTmx[i].path);
			}
			catch (TMXLoadException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	public TMXTiledMap newTiledMap(int index)
	{
		return newTiledMap(index, engine.getEngine().getScene());
	}
	
	public TMXTiledMap newTiledMap(int index, Scene scene)
	{
		Utils.TRACE("er = " +index);
		Utils.TRACE("er = " +tiledMap[index]);
		
		for (TMXLayer tmxLayer : tiledMap[index].getTMXLayers())
		{
			scene.attachChild(tmxLayer);
		}
		
		return tiledMap[index];
	}
}
